﻿//创建时间: 2010-10-25
//创建作者: 李 城
//功能说明: CMS函数接口         
//最后修改: 2010-11-9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using TRPOP.Common;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Plugin
{
    /// <summary>
    ///  CMS函数处理接口
    /// </summary>
    public interface IFunc
    {
        /// <summary>
        /// //date ( 格式字串, 时间戳)
        /// </summary>
        /// <param name="strFormat"></param>
        /// <param name="datetime"></param>
        /// <returns></returns>
        string date(string strFormat, DateTime datetime);
        /// <summary>
        /// 中文字符串截取函数，根据开始位置和截取长度，自动截取字符串的长度，如果字符串长度小于截取长度，则返回原字符串，否则返回截取字符串+后缀，默认后缀为“...”
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="intIndex"></param>
        /// <param name="intLength"></param>
        /// <returns></returns>
        string CsubStr(string strContent, int intIndex, int intLength);
        /// <summary>
        /// 中文字符串截取函数，根据开始位置和截取长度，自动截取字符串的长度，如果字符串长度小于截取长度，则返回原字符串，否则返回截取字符串+后缀，默认后缀为“...”
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="intIndex"></param>
        /// <param name="intLength"></param>
        /// <param name="append"></param>
        /// <returns></returns>
        string CsubStr(string strContent, int intIndex, int intLength, string append);



        /// <summary>
        /// (去除HTML标记)中文字符串截取函数，根据开始位置和截取长度，自动截取字符串的长度，如果字符串长度小于截取长度，则返回原字符串，否则返回截取字符串+后缀，默认后缀为“...”
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="intIndex"></param>
        /// <param name="intLength"></param>
        /// <returns></returns>
        string CsubStr_tags(string strContent, int intIndex, int intLength);

        /// <summary>
        /// (去除HTML标记)中文字符串截取函数，根据开始位置和截取长度，自动截取字符串的长度，如果字符串长度小于截取长度，则返回原字符串，否则返回截取字符串+后缀，默认后缀为“...”
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="intIndex"></param>
        /// <param name="intLength"></param>
        /// <param name="append"></param>
        /// <returns></returns>
        string CsubStr_tags(string strContent, int intIndex, int intLength, string append);


        /// <summary>
        /// 清空字符串中的回车换行符，常用于js输出
        /// </summary>
        /// <param name="strContent"></param>
        /// <returns></returns>
        string strip(string strContent);
        /// <summary>
        /// 重复显示字符串N次(重复次数)
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="intCount"></param>
        /// <returns></returns>
        string str_repeat(string strContent, int intCount);
        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="strContent"></param>
        /// <returns></returns>
        bool empty(string strContent);
        /// <summary>
        /// 查看某数据的数目
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        int count(object obj);
        ///// <summary>
        ///// 分页函数
        ///// </summary>
        ///// <param name="TotalPage"></param>
        ///// <param name="CurrentPage"></param>
        ///// <param name="URL"></param>
        ///// <param name="showPageIndex">显示的导航页数</param>
        ///// <returns></returns>
        //string list_page(string TotalPage, string CurrentPage, string URL, string showPageIndexCount, PageInfo m_pageinfo);
        /// <summary>
        /// 分页函数
        /// </summary>
        /// <param name="TotalPage"></param>
        /// <param name="CurrentPage"></param>
        /// <param name="URL"></param>
        /// <param name="showPageIndexCount"></param>
        /// <param name="strlistPageRegName"></param>
        /// <param name="intNodeId"></param>
        /// <param name="intContentId"></param>
        /// <param name="strSearchPageKeys"></param>
        /// <param name="oTemplateType"></param>
        /// <param name="intStaticPageNum"></param>
        /// <returns></returns>
        string list_page(string TotalPage, string CurrentPage, string URL, string showPageIndexCount, string strlistPageRegName, int intNodeId, int intContentId, string strSearchPageKeys, EnumList.TemplateType oTemplateType, int intStaticPageNum);
        /// <summary>
        /// 自动压缩图片
        /// </summary>
        /// <param name="strSourceUrl"></param>
        /// <param name="strWidthAndLength"></param>
        /// <returns></returns>
        string automini(string strSourceUrl, string strWidthAndLength);

        /// <summary>
        /// 自动压缩图片
        /// </summary>
        /// <param name="strSourceUrl"></param>
        /// <param name="intWidth"></param>
        /// <param name="intHeight"></param>
        /// <returns></returns>
        string autominiHttpToLocal(string strSourceUrl, int intWidth, int intHeight);
        /// <summary>
        /// 根据文件的相对地址得到文件的访问地址（以http开头的）
        /// </summary>
        /// <param name="strImgSource"></param>
        /// <returns></returns>
        string GetHttpURLForLocalFile(string strImgSource);

        /// <summary>
        /// 去除html标签，并返回指定的长度的字符串，
        /// </summary>
        /// <param name="strHTML"></param>
        /// <param name="length">如果为0 则返回所有</param>
        /// <returns></returns>
        string removeHTML(string strHTML, string length);

        /// <summary>
        /// 搜索关键字
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="strURL">{0} 是关键字需要Microsoft.JScript.GlobalObject.escape</param>
        /// <returns></returns>
        string searchSplit(string keys, string strURL);
        /// <summary>
        /// 搜索关键字
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="strURL">{0} 是关键字需要Microsoft.JScript.GlobalObject.escape</param>
        /// <param name="strPre"></param>
        /// <param name="strEnd"></param>
        /// <returns></returns>
        string searchSplit(string keys, string strURL, string strPre, string strEnd);
        /// <summary>
        /// 搜索关键字
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="strURL"></param>
        /// <param name="strPre"></param>
        /// <param name="strEnd"></param>
        /// <param name="strAParam"></param>
        /// <returns></returns>
        string searchSplit(string keys, string strURL, string strPre, string strEnd, string strAParam);

        /// <summary>
        /// 搜索关键字
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="strURL"></param>
        /// <param name="strPre"></param>
        /// <param name="strEnd"></param>
        /// <param name="strAParam"></param>
        /// <param name="intTopN"></param>
        /// <returns></returns>
        string searchSplit(string keys, string strURL, string strPre, string strEnd, string strAParam, int intTopN);

        /// <summary>
        /// 中文字符串截取函数，根据开始位置和截取长度，自动截取字符串的长度，如果字符串长度小于截取长度，则返回原字符串，否则返回截取字符串+后缀，默认后缀为“...”
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="intIndex"></param>
        /// <param name="intLength"></param>
        /// <returns></returns>
        string CsubStrForHtml(string strContent, int intIndex, int intLength);
        /// <summary>
        /// 中文字符串截取函数，根据开始位置和截取长度，自动截取字符串的长度，如果字符串长度小于截取长度，则返回原字符串，否则返回截取字符串+后缀，默认后缀为“...”
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="intIndex"></param>
        /// <param name="intLength"></param>
        /// <param name="append"></param>
        /// <returns></returns>
        string CsubStrForHtml(string strContent, int intIndex, int intLength, string append);
    }

    public class IFuncControl
    {
        /*
        public static object ExecuteFunction(string FuncName, ArrayList olistParams, ref bool isSucess, IFunc mIFunc, PageInfo m_pageinfo)
        {
            isSucess = false;
            object obj = "";
            string strError = "没有找到有" + olistParams.Count + "个参数的" + FuncName + "函数";

            #region
            switch (FuncName.Trim().ToLower())
            {
                case "date":// date('Y-m-d H:i:s', $var.PublishDate)
                    if (olistParams.Count == 2 || olistParams.Count == 1)
                    {
                        if (olistParams.Count == 1)
                            obj = mIFunc.date(olistParams[0].ToString(), DateTime.Now);
                        else
                            obj = mIFunc.date(olistParams[0].ToString(), olistParams[1].ToString().ToDateTime());
                        isSucess = true;
                    }
                    break;
                case "automini":// AutoMini($var.Photo,'120*100',$var)
                    if (olistParams.Count >= 2)
                    {
                        obj = mIFunc.automini(olistParams[0].ToString(), olistParams[1].ToString());
                        isSucess = true;
                        break;
                    }
                    break;
                case "gethttpurlforlocalfile"://gethttpurlforlocalfile('/imng/a.jgp')
                    if (olistParams.Count == 1)
                    {
                        obj = mIFunc.GetHttpURLForLocalFile(olistParams[0].ToString());
                        isSucess = true;
                        break;
                    }
                    break;
                case "autominihttptolocal":// autominihttptolocal($var.Photo,120,100)
                    if (olistParams.Count == 3)
                    {
                        obj = mIFunc.autominiHttpToLocal(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt());
                        isSucess = true;
                        break;
                    }
                    break;
                case "csubstr":// CsubStr($var.Title,0,10)  CsubStr($var.Title,0,10,"")
                    if (olistParams.Count == 3 || olistParams.Count == 4)
                    {
                        if (olistParams.Count == 3)
                            obj = mIFunc.CsubStr(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt());
                        else
                            obj = mIFunc.CsubStr(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt(), olistParams[3].ToString().Replace("\"", ""));
                        isSucess = true;
                    }
                    break;
                case "csubstrforhtml":// csubstrforhtml($var.Title,0,10)  csubstrforhtml($var.Title,0,10,"")
                    if (olistParams.Count == 3 || olistParams.Count == 4)
                    {
                        if (olistParams.Count == 3)
                            obj = mIFunc.CsubStrForHtml(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt());
                        else
                            obj = mIFunc.CsubStrForHtml(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt(), olistParams[3].ToString().Replace("\"", ""));
                        isSucess = true;
                    }
                    break;
                case "csubstr_tags":
                    if (olistParams.Count == 3 || olistParams.Count == 4)
                    {
                        if (olistParams.Count == 3)
                            obj = mIFunc.CsubStr_tags(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt());
                        else
                            obj = mIFunc.CsubStr_tags(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt(), olistParams[3].ToString().Replace("\"", ""));
                        isSucess = true;
                    }
                    break;

                case "strip":// strip($var.Title)
                    if (olistParams.Count == 1)
                    {
                        obj = mIFunc.strip(olistParams[0].ToString());
                        isSucess = true;
                    }
                    break;
                case "str_repeat":// str_repeat("<td><img src='/../images/star.gif' border='0'></td>",$Star)
                    if (olistParams.Count == 2)
                    {
                        obj = mIFunc.str_repeat(olistParams[0].ToString(), olistParams[1].ToString().ToInt());
                        isSucess = true;
                    }
                    break;
                case "empty":
                    if (olistParams.Count == 1)
                    {
                        obj = mIFunc.empty(olistParams[0].ToString());
                        isSucess = true;
                    }
                    break;
                case "!empty":
                    if (olistParams.Count == 1)
                    {
                        obj = !mIFunc.empty(olistParams[0].ToString());
                        isSucess = true;
                    }
                    break;
                case "count":
                    if (olistParams.Count == 1)
                    {
                        obj = mIFunc.count(olistParams[0]);
                        isSucess = true;
                    }
                    break;
                case "list_page":
                    //if (olistParams.Count == 3)
                    //{
                    //    obj = mIFunc.list_page(olistParams[0].ToString(), olistParams[1].ToString(), olistParams[2].ToString(), "10", m_pageinfo);
                    //    isSucess = true;
                    //}
                    break;
                case "removehtml":
                    if (olistParams.Count == 2)
                    {
                        obj = mIFunc.removeHTML(olistParams[0].ToString(), olistParams[1].ToString());
                        isSucess = true;
                    }
                    break;
                case "urlencode"://urlEncode
                    if (olistParams.Count == 1)
                    {
                        obj = olistParams[0].ToString().UrlEncode();
                        isSucess = true;
                    }
                    break;
                case "searchsplit":
                    if (olistParams.Count == 1)
                    {
                        obj = mIFunc.searchSplit(olistParams[0].ToString(),
                                                 SYSConfig.SystemVar_PageSearchURL + "?nodeid=" + m_pageinfo.NodeIdOut.ToString() + "&page=1&" + SYSConfig.SystemVar_SearchFormInputName + "={0}"
                                                 );
                        isSucess = true;
                    }
                    else if (olistParams.Count == 3)
                    {
                        obj = mIFunc.searchSplit(olistParams[0].ToString(),
                                                SYSConfig.SystemVar_PageSearchURL + "?nodeid=" + m_pageinfo.NodeIdOut.ToString() + "&page=1&" + SYSConfig.SystemVar_SearchFormInputName + "={0}",
                                                olistParams[1].ToString(),
                                                olistParams[2].ToString()
                                                );
                        isSucess = true;
                    }
                    break;
                default:
                    obj = strError = "没有找到" + FuncName + "函数";
                    break;
            }
            #endregion

            return obj;
        }
        */
        public static object ExecuteFunctionForDll(string FuncName, ArrayList olistParams, ref bool isSucess, IFunc mIFunc)
        {
            isSucess = false;
            object obj = "";
            string strError = "没有找到有" + olistParams.Count + "个参数的" + FuncName + "函数";

            #region
            switch (FuncName.Trim().ToLower())
            {
                case "date":// date('Y-m-d H:i:s', $var.PublishDate)
                    if (olistParams.Count == 2 || olistParams.Count == 1)
                    {
                        if (olistParams.Count == 1)
                            obj = mIFunc.date(olistParams[0].ToString(), DateTime.Now);
                        else
                            obj = mIFunc.date(olistParams[0].ToString(), olistParams[1].ToString().ToDateTime());
                        isSucess = true;
                    }
                    break;
                case "automini":// AutoMini($var.Photo,'120*100',$var)
                    if (olistParams.Count >= 2)
                    {
                        obj = mIFunc.automini(olistParams[0].ToString(), olistParams[1].ToString());
                        isSucess = true;
                        break;
                    }
                    break;
                case "gethttpurlforlocalfile"://gethttpurlforlocalfile('/imng/a.jgp')
                    if (olistParams.Count == 1)
                    {
                        obj = mIFunc.GetHttpURLForLocalFile(olistParams[0].ToString());
                        isSucess = true;
                        break;
                    }
                    break;
                case "autominihttptolocal":// autominihttptolocal($var.Photo,120,100)
                    if (olistParams.Count == 3)
                    {
                        obj = mIFunc.autominiHttpToLocal(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt());
                        isSucess = true;
                        break;
                    }
                    break;
                case "csubstr":// CsubStr($var.Title,0,10)  CsubStr($var.Title,0,10,"")
                    if (olistParams.Count == 3 || olistParams.Count == 4)
                    {
                        if (olistParams.Count == 3)
                            obj = mIFunc.CsubStr(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt());
                        else
                            obj = mIFunc.CsubStr(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt(), olistParams[3].ToString().Replace("\"", ""));
                        isSucess = true;
                    }
                    break;
                case "csubstrforhtml":// csubstrforhtml($var.Title,0,10)  csubstrforhtml($var.Title,0,10,"")
                    if (olistParams.Count == 3 || olistParams.Count == 4)
                    {
                        if (olistParams.Count == 3)
                            obj = mIFunc.CsubStrForHtml(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt());
                        else
                            obj = mIFunc.CsubStrForHtml(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt(), olistParams[3].ToString().Replace("\"", ""));
                        isSucess = true;
                    }
                    break;
                case "csubstr_tags":
                    if (olistParams.Count == 3 || olistParams.Count == 4)
                    {
                        if (olistParams.Count == 3)
                            obj = mIFunc.CsubStr_tags(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt());
                        else
                            obj = mIFunc.CsubStr_tags(olistParams[0].ToString(), olistParams[1].ToString().ToInt(), olistParams[2].ToString().ToInt(), olistParams[3].ToString().Replace("\"", ""));
                        isSucess = true;
                    }
                    break;

                case "strip":// strip($var.Title)
                    if (olistParams.Count == 1)
                    {
                        obj = mIFunc.strip(olistParams[0].ToString());
                        isSucess = true;
                    }
                    break;
                case "str_repeat":// str_repeat("<td><img src='/../images/star.gif' border='0'></td>",$Star)
                    if (olistParams.Count == 2)
                    {
                        obj = mIFunc.str_repeat(olistParams[0].ToString(), olistParams[1].ToString().ToInt());
                        isSucess = true;
                    }
                    break;
                case "empty":
                    if (olistParams.Count == 1)
                    {
                        obj = mIFunc.empty(olistParams[0].ToString());
                        isSucess = true;
                    }
                    break;
                case "!empty":
                    if (olistParams.Count == 1)
                    {
                        obj = !mIFunc.empty(olistParams[0].ToString());
                        isSucess = true;
                    }
                    break;
                case "count":
                    if (olistParams.Count == 1)
                    {
                        obj = mIFunc.count(olistParams[0]);
                        isSucess = true;
                    }
                    break;
                case "list_page":
                    //if (olistParams.Count == 3)
                    //{
                    //    obj = mIFunc.list_page(olistParams[0].ToString(), olistParams[1].ToString(), olistParams[2].ToString(), "10", m_pageinfo);
                    //    isSucess = true;
                    //}
                    //else 

                    if (olistParams.Count == 10)
                    {
                        obj = mIFunc.list_page(olistParams[0].ToString(), olistParams[1].ToString(), olistParams[2].ToString(), olistParams[3].ToString(), olistParams[4].ToString(), olistParams[5].ToString().ToInt(), olistParams[6].ToString().ToInt(), olistParams[7].ToString(), (EnumList.TemplateType)Enum.ToObject(typeof(EnumList.TemplateType), olistParams[8].ToString().ToInt()), olistParams[9].ToString().ToInt());
                        isSucess = true;
                    }
                    break;
                case "removehtml":
                    if (olistParams.Count == 2)
                    {
                        obj = mIFunc.removeHTML(olistParams[0].ToString(), olistParams[1].ToString());
                        isSucess = true;
                    }
                    break;
                case "urlencode"://urlEncode
                    if (olistParams.Count == 1)
                    {
                        obj = olistParams[0].ToString().UrlEncode();
                        isSucess = true;
                    }
                    break;
                case "searchsplit":
                    string strURL = string.Empty;
                    switch (olistParams.Count)
                    {
                        case 2:
                            strURL = SYSConfig.SystemVar_PageSearchURL + "?nodeid=" + olistParams[1].ToString().ToInt() + "&page=1&" + SYSConfig.SystemVar_SearchFormInputName + "={0}";
                            obj = mIFunc.searchSplit(olistParams[0].ToString(), strURL);
                            isSucess = true;
                            break;
                        case 4:  //searchsplit$NodeVar.SEOKey,"","&nbsp;&nbsp;")
                            strURL = SYSConfig.SystemVar_PageSearchURL + "?nodeid=" + olistParams[3].ToString().ToInt() + "&page=1&" + SYSConfig.SystemVar_SearchFormInputName + "={0}";
                            obj = mIFunc.searchSplit(olistParams[0].ToString(), strURL, olistParams[1].ToString(), olistParams[2].ToString()
                                                    );
                            isSucess = true;
                            break;
                        case 5: //searchsplit$NodeVar.SEOKey,"","&nbsp;&nbsp;","class='asd'")
                            strURL = SYSConfig.SystemVar_PageSearchURL + "?nodeid=" + olistParams[4].ToString().ToInt() + "&page=1&" + SYSConfig.SystemVar_SearchFormInputName + "={0}";
                            obj = mIFunc.searchSplit(olistParams[0].ToString(), strURL, olistParams[1].ToString(), olistParams[2].ToString(), olistParams[3].ToString());
                            isSucess = true;
                            break;
                        case 6: //searchsplit$NodeVar.SEOKey,"","&nbsp;&nbsp;","class='asd'",5)
                            strURL = SYSConfig.SystemVar_PageSearchURL + "?nodeid=" + olistParams[5].ToString().ToInt() + "&page=1&" + SYSConfig.SystemVar_SearchFormInputName + "={0}";
                            obj = mIFunc.searchSplit(olistParams[0].ToString(), strURL, olistParams[1].ToString(), olistParams[2].ToString(), olistParams[3].ToString(), olistParams[4].ToString().ToInt());
                            isSucess = true;
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    obj = strError = "没有找到" + FuncName + "函数";
                    break;
            }
            #endregion

            return obj;
        }
    }
}
